package net.zhengxinyun.performance.service.indicators;

import com.sirdc.modules.utils.ObjectUtils;
import net.zhengxinyun.performance.Util.StringUtils;
import net.zhengxinyun.performance.Util.Util;
import net.zhengxinyun.performance.dto.ExcelDTO;
import net.zhengxinyun.performance.dto.TownDTO;
import net.zhengxinyun.performance.entity.*;
import net.zhengxinyun.performance.filter.*;
import net.zhengxinyun.performance.mapper.Department.DepartmentEntityMapper;
import net.zhengxinyun.performance.mapper.Indicators.TownIndicatorsEntityMapper;
import net.zhengxinyun.performance.mapper.Indicators.TownMapper;
import net.zhengxinyun.performance.mapper.Indicators.TownScoreEntityMapper;
import net.zhengxinyun.performance.mapper.login.RolePerMapper;
import net.zhengxinyun.performance.service.department.DepartmentService;
import net.zhengxinyun.performance.service.login.RolePerService;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Auther: hyp
 * @Date: 2018/10/29 18:21
 */
@Service
public class TownIndicatorsServiceImpl implements TownIndicatorsService {

    private TownIndicatorsEntityMapper townIndicatorsEntityMapper;
    private TownMapper townMapper;
    private TownService townService;
    private TownScoreEntityMapper townScoreEntityMapper;
    private DepartmentEntityMapper departMapper;

    @Autowired
    private RolePerMapper rolePerMapper;


    @Autowired
    public TownIndicatorsServiceImpl(TownIndicatorsEntityMapper townIndicatorsEntityMapper, TownMapper townMapper,
                                     DepartmentEntityMapper departMapper,TownService townService, TownScoreEntityMapper townScoreEntityMapper) {
        this.townIndicatorsEntityMapper = townIndicatorsEntityMapper;
        this.townMapper = townMapper;
        this.townService = townService;
        this.townScoreEntityMapper = townScoreEntityMapper;
        this.departMapper=departMapper;
    }

    @Override
    @Transactional
    public String delete(TownIndicatorsEntity townIndicatorsEntity) {
        int a;
        TownIndicatorsEntity entity = townIndicatorsEntityMapper.selectByPrimaryKey(townIndicatorsEntity.getId());
        if ("一级指标".equals(entity.getIndexType())) {
            TownIndicatorsFilter townIndicatorsFilter = new TownIndicatorsFilter();
            townIndicatorsFilter.setSuperiorIndexId(entity.getId().toString());
            List<TownIndicatorsFilter> list = townIndicatorsEntityMapper.query(townIndicatorsFilter);
            for (TownIndicatorsFilter filter : list) {
                a = townIndicatorsEntityMapper.deleteByPrimaryKey(filter.getId());
                if (a == 0) {
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    return "下属指标删除失败";
                }
                townScoreEntityMapper.deleteByIndexId(filter.getId().toString());
            }
        }
        a = townIndicatorsEntityMapper.deleteByPrimaryKey(townIndicatorsEntity.getId());
        if (a == 0) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return "删除失败,请联系管理员";
        }
        return "删除成功";
    }

    @Override
    @Transactional
    public String update(TownIndicatorsEntity townIndicatorsEntity, List<TownEntity> townList) {
        Long id = townIndicatorsEntity.getId();
        String dateTime = townIndicatorsEntity.getDateTime();
        //修改牵头单位
        int i1 = townIndicatorsEntityMapper.updateByPrimaryKeySelective(townIndicatorsEntity);

        //修改权重
        for (TownEntity entity : townList) {
            TownEntity townWeight = townMapper.selectTownWeright(id + "", entity.getDepartcode(), dateTime);
            if(ObjectUtils.isNotBlank(townWeight)){
                    townWeight.setWeight(entity.getWeight());
                int i = townMapper.updateByPrimaryKey(townWeight);
            }

        }




        return "更新成功";
    }

    /**
     * 单个指标添加
     *
     * @param townIndicatorsEntity
     * @param townList
     * @return
     */
    @Override
    @Transactional
    public String insert(TownIndicatorsEntity townIndicatorsEntity, List<TownEntity> townList) {
        //如果没有乡镇就直接添加
        if (townList == null) {
            setSuperId(townIndicatorsEntity);
            //手动添加设置参数为有效状态
            townIndicatorsEntity.setAvail(1);
            long i = townIndicatorsEntityMapper.insert(townIndicatorsEntity);
            if (i != 1) {
                //触发事务回滚
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return "添加失败,请联系管理员";
            }
            return "添加成功";
        }
        //如果是二级指标
        if ("二级指标".equals(townIndicatorsEntity.getIndexType())) {
           // setSuperId(townIndicatorsEntity);
            //手动添加设置参数为有效状态
            townIndicatorsEntity.setAvail(1);
            long i = townIndicatorsEntityMapper.insert(townIndicatorsEntity);
            if (i != 1) {
                //触发事务回滚
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return "添加失败,请联系管理员";
            }
            String datetime = townIndicatorsEntity.getDateTime();
          //  townService.insert(townIndicatorsEntity, townList,datetime);
            Long id = townIndicatorsEntity.getId();
            //设置年份
            townList.forEach(entity -> entity.setDatetime(datetime));
            townList.forEach(entity -> entity.setIndexId(id+""));

            townMapper.batchSaveTownWeight(townList);

        }
        return "添加成功";
    }

    /**
     * 批量添加
     *
     * @param list
     * @return
     */
    @Override
    @Transactional
    public String ListInsert(List<TownDTO> list) {
        list = removeDuplicate(list);
        for (TownDTO townDTO : list) {
            TownIndicatorsEntity townIndicatorsEntity = townDTO.getTownIndicatorsEntity();
            List<TownEntity> townList = townDTO.getTownList();
            if ("二级指标".equals(townIndicatorsEntity.getIndexType())) {
                setSuperId(townIndicatorsEntity);
                DepartmentEntity depart = departMapper.getDepartByName(townIndicatorsEntity.getLeadUnit()); //牵头单位 id；
                if(ObjectUtils.isBlank(depart))
                {
                    System.out.println("---------------找不到牵头单位-------------"+townIndicatorsEntity.getLeadUnit()+"--------------------------");
                    continue;
                }
                townIndicatorsEntity.setLeadUnit(depart.getDepartcode());

                depart = departMapper.getDepartByName(townIndicatorsEntity.getResponsibilityUnit()); //责任 id；
                if(ObjectUtils.isBlank(depart))
                {
                    System.out.println("----------------找不到责任单位------------"+townIndicatorsEntity.getResponsibilityUnit()+"--------------------------");
                    continue;
                }
                townIndicatorsEntity.setResponsibilityUnit(depart.getDepartcode());

               /* depart = departMapper.getDepartByName(townIndicatorsEntity.getu()); //牵头单位 id；
                townIndicatorsEntity.setLeadUnit(depart.getDepartcode());*/

                townIndicatorsEntity.setAvail(1);
                long i = townIndicatorsEntityMapper.insert(townIndicatorsEntity);
                if (i != 1) {
                    //触发事务回滚
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    return "添加失败,请联系管理员";
                }
                String datatime =townIndicatorsEntity.getDateTime();
                townService.insert(townIndicatorsEntity, townList,datatime);



            } else if ("一级指标".equals(townIndicatorsEntity.getIndexType())) {
                TownIndicatorsFilter filter = townIndicatorsEntityMapper.queryByNameAndTime(townIndicatorsEntity);
                if (filter != null) {
                    continue;
                }
                townIndicatorsEntity.setAvail(1);
                long i = townIndicatorsEntityMapper.insert(townIndicatorsEntity);
                if (i != 1) {
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    return "添加失败。请联系管理员";
                }
            }
        }
        return "添加成功";
    }

    /**
     * 查询二级指标及其对应的乡镇
     *
     * @param townIndicatorsFilter
     * @return
     */
    @Override
    public List<Map<String, Object>> querySec(TownIndicatorsFilter townIndicatorsFilter, UserFilter userEntity) {

        /*
         *县直单位查看自己的指标
         */
     /*
        按照勾选状态来筛选。
      if("县直单位".equals(userEntity.getDepartType())){
           townIndicatorsFilter.setLeadUnit(userEntity.getDepartcode());
       }*/

        /**
         * 筛选指标
         */
        List<TownIndicatorsFilter> listtemp = townIndicatorsEntityMapper.query(townIndicatorsFilter);
        List<Map<String, Object>> indexList = new ArrayList<>();
        TownIndicatorsEntity entity;

        /**
         * 所有部门
         */
     /*   List<DepartmentFilter> departlist  = departMapper.query(new DepartmentFilter());
        HashMap<String,String> nameToCode = new HashMap<>();
        HashMap<String,String> codeToName = new HashMap<>();*/
        /*for(DepartmentFilter item: departlist)
        {
            nameToCode.put(item.getDepartAddr(),item.getDepartcode());
            codeToName.put(item.getDepartcode(),item.getDepartAddr());
        }*/

//         可查看的指标
        RolePerEntity roles = rolePerMapper.selectByPrimaryKey(userEntity.getRoleid());
        String strindex =null;
        if(ObjectUtils.isBlank(roles))
        {
            strindex = userEntity.getIndexRight();
        }else {
            strindex = roles.getIndexRight();
        }

        List<TownIndicatorsFilter> list = new ArrayList<>();
        if("admin".equals(userEntity.getUserPermissions()))
        {
            list = listtemp;
        }else {
            HashMap<String,Object> indexIdMap = Util.getIndexIdMap(strindex);
            //        过滤可查看的指标

            for (TownIndicatorsFilter item: listtemp)
            {
                String id1 = item.getId().toString();
                Object ob = indexIdMap.get(id1);
                if(ObjectUtils.isBlank(ob))
                    continue;
                String id2 = indexIdMap.get(id1).toString();

                if(id1.equals(id2))
                {
                    list.add(item);
                }
            }
        }

//        封装数据

        for (TownIndicatorsFilter filter : list) {
            Map<String, Object> indexMap = new LinkedHashMap<>();
            indexMap.put("id", filter.getId());
            indexMap.put("indexType", filter.getIndexType());
            indexMap.put("leadUnit",filter.getLeadUnit() );
            indexMap.put("leadUnitName",filter.getLeadUnitName());
            indexMap.put("indexName", filter.getIndexName());
            indexMap.put("responsibilityUnit", filter.getResponsibilityUnit());
            indexMap.put("responsibilityUnitCode",filter.getResponsibilityUnit());
            indexMap.put("direction", filter.getDirection());
            indexMap.put("weight",   StringUtils.isBlank(filter.getWeight())?"": Double.parseDouble(filter.getWeight()));
            indexMap.put("dateTime", filter.getDateTime());
            indexMap.put("mainUnit", filter.getMainUnit());
            indexMap.put("superiorIndexId",filter.getSuperiorIndexId());
            //把对应的上级指标名返回给前端
         /*   if (StringUtils.isNotBlank(filter.getSuperiorIndexId())) {
                entity = townIndicatorsEntityMapper.selectByPrimaryKey(Long.valueOf(filter.getSuperiorIndexId()));
                if (entity != null) {
                    indexMap.put("superiorIndexId", entity.getIndexName());
                }
            }*/
            List<TownFilter> townList = townMapper.queryByIndexId(filter.getId().toString());
            for (TownFilter townFilter : townList) {
                if (StringUtils.isNotBlank(townFilter.getWeight())) {
                    indexMap.put(townFilter.getTownName(), Double.parseDouble(townFilter.getWeight()));
                } else {
                    indexMap.put(townFilter.getTownName(), null);
                }
            }
            indexList.add(indexMap);
        }
        return indexList;
    }

    @Override
    public List<Map<String, Object>> queryIndexMap(List<TownIndicatorsFilter> list) {
        List<Map<String, Object>> indexList = new ArrayList<>();
        TownIndicatorsFilter townIndicatorsFilter = new TownIndicatorsFilter();
        for (TownIndicatorsFilter filter : list) {
            HashMap indexMap = new HashMap();
            indexMap.put("firstName", filter);
            townIndicatorsFilter.setIndexType("");
            townIndicatorsFilter.setSuperiorIndexId(filter.getId().toString());
            List<TownIndicatorsFilter> secondIndexList = query(townIndicatorsFilter);
            indexMap.put("secondIndex", secondIndexList);
            indexList.add(indexMap);
        }
        return indexList;
    }

    @Override
    public List<TownIndicatorsFilter> query(TownIndicatorsFilter townIndicatorsFilter) {
        return townIndicatorsEntityMapper.query(townIndicatorsFilter);
    }

    @Override
    public Integer queryForCount(TownIndicatorsFilter townIndicatorsFilter) {
        return townIndicatorsEntityMapper.queryForCount(townIndicatorsFilter);
    }

    /**
     * 设置上级指标id
     *
     * @param townIndicatorsEntity
     * @return
     */
    public TownIndicatorsEntity setSuperId(TownIndicatorsEntity townIndicatorsEntity) {
        if (StringUtils.isNotBlank(townIndicatorsEntity.getSuperiorIndexId())) {
            String id = townIndicatorsEntityMapper.queryIdBySuperName(townIndicatorsEntity);
            townIndicatorsEntity.setSuperiorIndexId(id);
        }
        return townIndicatorsEntity;
    }

    @Override
    public List<TownIndicatorsFilter> indexTown(IndexFilter indexFilter) {
        List<TownIndicatorsFilter> list = null;
        if (StringUtils.isBlank(indexFilter.getDateTime()) && StringUtils.isBlank(indexFilter.getMonthTime())){
            TownScoreFilter townScoreFilter = townScoreEntityMapper.queryForNewTime();
            indexFilter.setDateTime(townScoreFilter.getDateTime());
            indexFilter.setMonthTime(townScoreFilter.getMonthTime());
            list = townIndicatorsEntityMapper.indexTown(indexFilter);
        }else {
            list = townIndicatorsEntityMapper.indexTown(indexFilter);
        }
        Collections.sort(list, new Comparator<TownIndicatorsFilter>() {
            @Override
            public int compare(TownIndicatorsFilter o1, TownIndicatorsFilter o2) {
                if (Integer.parseInt(o1.getRank()) > Integer.parseInt(o2.getRank())){
                    return 1;
                }else {
                    return -1;
                }
            }
        });
        return list;
    }

    @Override
    public List<Map<String, Object>> getIndexMap(List<TownIndicatorsFilter> list) {
        List<Map<String, Object>> indexList = new ArrayList<>();
        TownIndicatorsFilter countryIndicatorsFilter = new TownIndicatorsFilter();
        List<TownIndicatorsFilter> copyList = new ArrayList<>();
        for (TownIndicatorsFilter filter : list) {
            HashMap indexMap = new HashMap();
            indexMap.put("firstName", filter);
            countryIndicatorsFilter.setIndexType("");
            countryIndicatorsFilter.setSuperiorIndexId(filter.getId().toString());
            List<TownIndicatorsFilter> secondIndexList = query(countryIndicatorsFilter);
            Iterator<TownIndicatorsFilter> it = secondIndexList.iterator();
            while (it.hasNext()) {
                TownIndicatorsFilter c = it.next();
                if (StringUtils.isBlank(c.getLeadUnit())) {
                    copyList.add(c);
                    it.remove();
                }
            }
            for (TownIndicatorsFilter f:copyList) {
                countryIndicatorsFilter.setSuperiorIndexId(f.getId().toString());
                List<TownIndicatorsFilter> sonList = query(countryIndicatorsFilter);
                secondIndexList.addAll(sonList);
            }
            indexMap.put("secondIndex", secondIndexList);
            indexList.add(indexMap);
        }
        return indexList;
    }

    @Override
    public TownIndicatorsEntity selectIndexById(Long id) {
        return townIndicatorsEntityMapper.selectByPrimaryKey(id);
    }

    /**
     * list集合根据上级指标id，指标名对元素去重
     *
     * @param list
     * @return
     */
    private List<TownDTO> removeDuplicate(List<TownDTO> list) {
        Set<TownDTO> set = new TreeSet<TownDTO>(new Comparator<TownDTO>() {
            @Override
            public int compare(TownDTO o1, TownDTO o2) {
                //字符串,则按照asicc码升序排列
                if (o1.getTownIndicatorsEntity().getIndexName().equals(o2.getTownIndicatorsEntity().getIndexName())
                        && o1.getTownIndicatorsEntity().getSuperiorIndexId().equals(o2.getTownIndicatorsEntity().getSuperiorIndexId())) {
                    return 0;
                }
                return 1;
            }
        });
        set.addAll(list);
        return new ArrayList<TownDTO>(set);
    }


    @Override
    public String  getIndexByDatetime(String dateTtime,String indexType) {

        String str ="";
        List<TownIndicatorsEntity> list =  townIndicatorsEntityMapper.getIndexByDatetime(dateTtime,indexType);
        if ( ObjectUtils.isNotBlank(list))
        {
            for(TownIndicatorsEntity item : list) {
                int i = townIndicatorsEntityMapper.deleteByPrimaryKey(item.getId());
                if(i==0)
                {
                    str = "失败";

                }

            }
        }

        return str;
    }


    @Override
    public int saveExcelDate(List<ExcelDTO> list,String datetime ) {

        List<TownIndicatorsEntity> oneIndexList  = new ArrayList<>();
        List<TownIndicatorsEntity>  twoIndexList  = new ArrayList<>();
        List<TownEntity> weightList = new ArrayList<>();
        int i=1;
        for (ExcelDTO dto : list) {
            TownIndicatorsEntity oneIndex = new TownIndicatorsEntity();
            TownIndicatorsEntity twoIndex = new TownIndicatorsEntity();

            oneIndex.setIndexName(dto.getCell0());
            oneIndex.setDateTime(datetime);
            oneIndex.setAvail(1);
            oneIndex.setIndexType("一级指标");

            twoIndex.setIndexName(dto.getCell1());
            twoIndex.setDateTime(datetime);
            twoIndex.setAvail(1);
            twoIndex.setSuperiorIndexId(dto.getCell0());
            twoIndex.setLeadUnit(dto.getCell23());
            twoIndex.setLeadUnitName(dto.getName23());
            twoIndex.setResponsibilityUnit(dto.getCell24());
            twoIndex.setIndexType("二级指标");
            twoIndex.setSort(i);
            i++;


            List<TownEntity> listweight = new ArrayList<>();



            //塘前乡
            String head=null;
            head =  dto.getCell2();
            TownEntity weight = new TownEntity();
            weight.setWeight(head);
            weight.setDatetime(datetime);
            weight.setDepartcode("1001");
            listweight.add(weight);

            //葛岭镇
            head =    head =  dto.getCell3();;
            weight = new TownEntity();
            weight.setWeight(head);
            weight.setDatetime(datetime);
            weight.setDepartcode("1002");
            listweight.add(weight);

            //樟城镇
            head =  dto.getCell4();
            weight = new TownEntity();
            weight.setWeight(head);
            weight.setDatetime(datetime);
            weight.setDepartcode("1003");
            listweight.add(weight);
            //城峰镇
            head =dto.getCell5();
            weight = new TownEntity();
            weight.setWeight(head);
            weight.setDatetime(datetime);
            weight.setDepartcode("1004");
            listweight.add(weight);
            //清凉镇
            head = dto.getCell6();
            weight = new TownEntity();
            weight.setWeight(head);
            weight.setDatetime(datetime);
            weight.setDepartcode("1005");
            listweight.add(weight);
            //富泉乡
            head = dto.getCell7();
            weight = new TownEntity();
            weight.setWeight(head);
            weight.setDatetime(datetime);
            weight.setDepartcode("1006");
            listweight.add(weight);
            //岭路乡
            head = dto.getCell8();
            weight = new TownEntity();
            weight.setWeight(head);
            weight.setDatetime(datetime);
            weight.setDepartcode("1007");
            listweight.add(weight);
            //赤锡乡
            head =  dto.getCell9();
            weight = new TownEntity();
            weight.setWeight(head);
            weight.setDatetime(datetime);
            weight.setDepartcode("1093");
            listweight.add(weight);
            //梧桐镇
            head =  dto.getCell10();
            weight = new TownEntity();
            weight.setWeight(head);
            weight.setDatetime(datetime);
            weight.setDepartcode("1009");
            listweight.add(weight);

            //嵩口镇
            head = dto.getCell11();
            weight = new TownEntity();
            weight.setWeight(head);
            weight.setDatetime(datetime);
            weight.setDepartcode("1010");
            listweight.add(weight);
            //洑口乡
            head = dto.getCell12();
            weight = new TownEntity();
            weight.setWeight(head);
            weight.setDatetime(datetime);
            weight.setDepartcode("1011");
            listweight.add(weight);
            //盖洋乡
            head = dto.getCell13();
            weight = new TownEntity();
            weight.setWeight(head);
            weight.setDatetime(datetime);
            weight.setDepartcode("1012");
            listweight.add(weight);
            //长庆镇
            head =  dto.getCell14();
            weight = new TownEntity();
            weight.setWeight(head);
            weight.setDatetime(datetime);
            weight.setDepartcode("1013");
            listweight.add(weight);
            //东洋
            head =  dto.getCell15();
            weight = new TownEntity();
            weight.setWeight(head);
            weight.setDatetime(datetime);
            weight.setDepartcode(1014+"");
            listweight.add(weight);
            //霞拔乡
            head =  dto.getCell16();
            weight = new TownEntity();
            weight.setWeight(head);
            weight.setDatetime(datetime);
            weight.setDepartcode(1015+"");
            listweight.add(weight);
            //同安镇
            head =  dto.getCell17();
            weight = new TownEntity();
            weight.setWeight(head);
            weight.setDatetime(datetime);
            weight.setDepartcode("1016");
            listweight.add(weight);
            //大洋镇
            head =  dto.getCell18();
            weight = new TownEntity();
            weight.setWeight(head);
            weight.setDatetime(datetime);
            weight.setDepartcode("1017");
            listweight.add(weight);
            //盘古乡
            head =  dto.getCell19();
            weight = new TownEntity();
            weight.setWeight(head);
            weight.setDatetime(datetime);
            weight.setDepartcode("1018");
            listweight.add(weight);
            //红星
            head =  dto.getCell20();
            weight = new TownEntity();
            weight.setWeight(head);
            weight.setDatetime(datetime);
            weight.setDepartcode("1019");
            listweight.add(weight);
            //白云
            head =dto.getCell21();
            weight = new TownEntity();
            weight.setWeight(head);
            weight.setDatetime(datetime);
            weight.setDepartcode("1020");
            listweight.add(weight);
            //丹云
            head = dto.getCell22();
            weight = new TownEntity();
            weight.setWeight(head);
            weight.setDatetime(datetime);
            weight.setDepartcode("1021");
            listweight.add(weight);

            twoIndex.setWeightList(listweight);
            System.out.println(listweight.size());
            twoIndexList.add(twoIndex);
            oneIndexList.add(oneIndex);
        }

        //一级指标去重
        oneIndexList = oneIndexList.stream().collect(
                Collectors.collectingAndThen(
                        Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(TownIndicatorsEntity::getIndexName))  ) ,ArrayList::new)
        );

        // 删除指标；
        townIndicatorsEntityMapper.deleteOldIndex(datetime);
        townMapper.deleteOldWeight(datetime);

        //保存一级指标

        int t = townIndicatorsEntityMapper.batchSaveIndex(oneIndexList);
        if(t==0)
        {
            return 0;
        }
        Map<String,String> oneIndexMap = oneIndexList.stream().collect(Collectors.toMap(o -> o.getIndexName(), o -> o.getId().toString() ));
        //设置二级指标的父级ID
        for (TownIndicatorsEntity entity : twoIndexList) {
           if(oneIndexMap.containsKey(entity.getSuperiorIndexId()))
           {
               entity.setSuperiorIndexId(oneIndexMap.get(entity.getSuperiorIndexId()));
           }
        }
        //保存二级指标
        int t2 =townIndicatorsEntityMapper.batchSaveIndex(twoIndexList);
        if(t2==0)
        {
            return 0;
        }

        for (TownIndicatorsEntity entity : twoIndexList) {

            List<TownEntity> townWeight = entity.getWeightList();
            for (TownEntity townEntity : townWeight) {
                townEntity.setIndexId(entity.getId().toString());
            }
            //保存权重

            int i1 = townMapper.batchSaveTownWeight(townWeight);
            if(i1 ==0)
            {
                return 0;
            }

        }




        return 1;
    }
}
